package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 太平洋电脑网
 * @author Persh
 *
 */
public class PconlineNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://[a-z]*.pconline.com.cn/[0-9]*/[0-9]*.html",
		"http://[a-z]*.pconline.com.cn/.*/[0-9]*/[0-9]*.html"
	};
	
	
	
	public boolean isDetailPage(String url) {
		if(!url.startsWith("http://product.pconline.com.cn/itbk/") 
				&& !url.startsWith("http://pcedu.pconline.com.cn/")
				&& !url.startsWith("http://diy.pconline.com.cn/")){
			for (int i=0; i < regex.length; i++) {
				if (url.matches(regex[i])) {
					return true;
				}
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.art_hd h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div.art_con_top h1").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("span.artTit").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("strong.artTit").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("h1").text();
					}
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.art_con"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.artBox"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.Ptext"));
				}
			}
		}
		date=DateUtils.matchDate(doc.select("div.art_hd p").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.art_con_top p").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("p.info").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.author").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("span.sTime").text());
						if(date==null){
							date=DateUtils.matchDate(doc.select("div.art").text());
						}
					}
				}
			}
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		PconlineNewsAnalyse analyse=new PconlineNewsAnalyse();
		String url="http://ln.pconline.com.cn/351/3516836.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
